dojo.require("dijit.dijit"); // optimize: load dijit layer
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dojox.grid.DataGrid");
dojo.require("dijit.form.Button");
dojo.require("dijit.Dialog");
dojo.require("dojo.parser"); // scan page for widgets

var load_finished_interval;
var n_deleted=0;

var delete_holiday_start = null;
var delete_holiday_end = null;
var delete_item = null;

dojo.addOnLoad( function()
{
	dialog_deleteEntry.closeButtonNode.style.display = "none";
	
	// hide loading gif
	window.setTimeout(function()
	{
		document.getElementById('img').style.display='none';
		document.getElementById('form').style.visibility='';
	}, 100);
	
	load_finished_interval = window.setInterval(function() {
		if (store_leaves._loadFinished == true)
		{
			load_finished();
			window.clearInterval(load_finished_interval);
		}
	}, 30);
});

var load_finished = function()
{
	if (store_leaves._arrayOfAllItems.length-n_deleted == 0)
	{
		window.setTimeout(function() { dialog_empty.show(); }, 200);
	}
};

var format_status = function(plain)
{
	if (plain == "") plain = "accepted";
	return '<span class="status_'+plain+'">'+plain+'</span>';
};

var format_options = function(st)
{
	if (st == "requested")
	{
		return '<button class="btn_delete" onClick="delete_entry('+arguments[1]+')">&nbsp;</button>';
	} else
	{
		return '';
	}
};

var delete_entry = function(rowIndex)
{
	var itm = grid_leaves.getItem(rowIndex);
	delete_item = itm;
	delete_holiday_start = store_leaves.getValue(itm, "start");
	delete_holiday_end = store_leaves.getValue(itm, "end");
	dialog_deleteEntry.show();
};

var delete_submit = function()
{
	dojo.byId('dlg_1').style.display = 'none';
	dojo.byId('dlg_2').style.display = '';
	
	dojo.xhrGet({
	    url: "html?action=del&start="+delete_holiday_start+"&end="+delete_holiday_end+"&mdl=leave_account",
	    handleAs: "text",
	    // timeout: 20000, infinity
	    load: function(rdata, ioArgs){
			var remainingLeave = dojo.byId("remainingLeave");
			remainingLeave.innerHTML = parseFloat(remainingLeave.innerHTML) + parseFloat(store_leaves.getValue(delete_item, "length"));
	        store_leaves.deleteItem(delete_item);
	        dialog_deleteEntry.hide();
	    	dojo.byId('dlg_2').style.display = 'none';
	    	dojo.byId('dlg_1').style.display = '';
	    	n_deleted++;
			load_finished();

			if (rdata != 'null')
			{
				alert ("Error while saving data. Please try again later. ("+rdata+")");
			}
	        return rdata;
	    },
	    error: function(response, ioArgs){
	    	alert ("Error while saving data. Please try again later. ("+arguments[0]+")");
	    	dojo.byId('dlg_2').style.display = 'none';
	    	dojo.byId('dlg_1').style.display = '';
	        return response;
	    }
	});
};

var go_year = function(y)
{
	dojo.byId("sel_year_cur").innerHTML = y;
	
	var url = '?mdl=leave_account&action=read&year='+y;
	try {
		grid_leaves.store.close();
	} catch (e)
	{
		grid_leaves.store.revert();
		go_year(y);
		return;
	}
	
	n_deleted=0;
	if (cur_year > y)
	{
		dojo.byId('remainingLeave').innerHTML = '';
		dojo.byId('remainingLeavePrevYear').innerHTML = '';
	} else if (cur_year == y)
	{
		dojo.byId('remainingLeave').innerHTML = remainingLeave;
		dojo.byId('remainingLeavePrevYear').innerHTML = remainingLeavePrev;
	} else
	{
		// future
		dojo.byId('remainingLeavePrevYear').innerHTML = '';
		dojo.xhrGet({
		    url: "html?action=remaining&mdl=leave_account&year="+y,
		    handleAs: "text",
		    // timeout: 20000, infinity
		    load: function(rdata, ioArgs){
				dojo.byId('remainingLeave').innerHTML = rdata;
		        return rdata;
		    },
		    error: function(response, ioArgs){
				dojo.byId('remainingLeave').innerHTML = '';
		        return response;
		    }
		});
	}
	
	grid_leaves.store.url = url;
	grid_leaves.store._jsonFileUrl = url;
	grid_leaves.sort();
	year = y;
	
	load_finished_interval = window.setInterval(function() {
		if (store_leaves._loadFinished == true)
		{
			load_finished();
			window.clearInterval(load_finished_interval);
		}
	}, 30);
};
